//
//  main.m
//  遍历
//
//  Created by 李煜 on 2024/1/29.
//https://blog.csdn.net/wsh7365062/article/details/51799538

#import <Foundation/Foundation.h>
void useFor(void);
void iteratorWithEnumerator(void);

void iteratorWithForIn(void);

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //        useFor();
        iteratorWithEnumerator();
    }
    return 0;
}
void useFor(void) {
    //处理数组//
    NSArray *arrayM = @[@"1",@"2",@"3",@"4"];
    NSInteger arrayMCount = [arrayM count];
    for (int i = 0; i<arrayMCount; i++) {
        NSString *obj = arrayM[i];
        NSLog(@"数组--%@",obj);
    }

    //字典 forin
    NSDictionary *dic1 = @{@"name":@"Tom",
                           @"age":@"19",
                           @"sex":@"男",
                           @"hobby":@"做饭"
    };

    for (NSString* key in dic1 ) {
        NSLog(@"字典---%@",key);
    }

    //        NSSet

    //处理集合//
    NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
    NSArray *setObjArray = [setM allObjects];
    for (int i = 0; i<setObjArray.count; i++) {
        NSString *obj = setObjArray[i];
        NSLog(@"集合---%@",obj);
    }

    //反向遍历----降序遍历----以数组为例
    NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"];
    NSInteger arrayMCount2 = [arrayM2 count] - 1;

    for (NSInteger i = arrayMCount2; i>0; i--) {
        NSString *obj = arrayM2[i];
        NSLog(@"数组反向---%@",obj);

    }

}

// 使用NSEnumerator遍历
void iteratorWithEnumerator(void) {
    //处理数组//
    NSArray *arrayM = @[@"1",@"2",@"3",@"4"];
    NSEnumerator *arrayEnumerator = [arrayM objectEnumerator];
    NSString *obj;
    while ((obj = [arrayEnumerator nextObject]) != nil) {
        NSLog(@"%@",obj);
    }

    //处理字典//
    NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"};
    NSEnumerator *dictEnumerator = [dictM keyEnumerator];
    NSString *key;
    while ((key = [dictEnumerator nextObject]) != nil) {
        NSString *obj = dictM[key];
        NSLog(@"===%@",obj);
    }


    //处理集合//
    NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
    NSEnumerator *setEnumerator = [setM objectEnumerator];
    NSString *setObj;
    while ((setObj = [setEnumerator nextObject]) != nil) {
        NSLog(@"%@",setObj);
    }


    //反向遍历----降序遍历----以数组为例
    NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"];
    NSEnumerator *arrayEnumerator2 = [arrayM2 reverseObjectEnumerator];
    NSString *obj2;
    while ((obj2 = [arrayEnumerator2 nextObject]) != nil) {
        NSLog(@"%@",obj2);
    }

}


void iteratorWithForIn(void) {
    //处理数组//
    NSArray *arrayM = @[@"1",@"2",@"3",@"4"];
    for (id obj in arrayM) {
        NSLog(@"%@",obj);
    }

    //处理字典//
    NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"};
    for (id obj in dictM) {
        NSLog(@"%@",dictM[obj]);
    }

    //处理集合//
    NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
    for (id obj in setM) {
        NSLog(@"%@",obj);
    }

    //反向遍历----降序遍历----以数组为例
    NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"];
    for (id obj in [arrayM2 reverseObjectEnumerator]) {
        NSLog(@"%@",obj);
    }
}

